include $(TOPDIR)/rules.mk

PKG_NAME:=docker-ce
PKG_VERSION:=19.03.12
PKG_RELEASE:=2
PKG_LICENSE:=Apache-2.0
PKG_LICENSE_FILES:=components/cli/LICENSE components/engine/LICENSE

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/docker/docker-ce/tar.gz/v$(PKG_VERSION)?
PKG_HASH:=d321f959e41fd9b24c23d95779d30d9ead1120bfddedf157ef5c047cf3d997a1
PKG_SOURCE_VERSION:=48a66213fe # SHA1 used within the docker executables

PKG_MAINTAINER:=Gerard Ryan <G.M0N3Y.2503@gmail.com>

# $(1) = path to dependent package 'Makefile'
# $(2) = relevant docker-ce '.installer' file
define EnsureVendoredVersion
	( \
		DEP_VER=$$$$( grep --only-matching --perl-regexp '(?<=PKG_SOURCE_VERSION:=)(.*)' "$(1)" ); \
		VEN_VER=$$$$( grep --only-matching --perl-regexp '(?<=_COMMIT:=)(.*)(?=})' "$(PKG_BUILD_DIR)/components/engine/hack/dockerfile/install/$(2)" ); \
		if [ $$$$VEN_VER != $$$$DEP_VER ]; then \
			echo "ERROR: Expected 'PKG_SOURCE_VERSION:=$$$$VEN_VER' in '$(1)', found 'PKG_SOURCE_VERSION:=$$$$DEP_VER'"; \
			exit 1; \
		fi \
	)
endef

PKG_BUILD_DEPENDS:=golang/host
PKG_BUILD_PARALLEL:=1

GO_PKG:=github.com/docker

include $(INCLUDE_DIR)/package.mk
include ../../lang/golang/golang-package.mk

define Package/docker-ce/config
  source "$(SOURCE)/Config.in"
endef

define Package/docker-ce
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=Docker Community Edition
  URL:=https://www.docker.com/
  DEPENDS:=$(GO_ARCH_DEPENDS) @(aarch64||arm||x86_64) +btrfs-progs +ca-certificates +cgroupfs-mount +containerd +libdevmapper +libnetwork +tini \
           +DOCKER_SECCOMP:libseccomp +iptables-mod-extra +kmod-br-netfilter +kmod-ikconfig +kmod-nf-conntrack-netlink +kmod-nf-ipvs +kmod-veth
  USERID:=docker:docker
  MENU:=1
endef

define Package/docker-ce/conffiles
/etc/config/dockerd
endef

define Package/docker-ce/description
  Docker Engine is used by millions enables containerized applications
  to run anywhere consistently on any infrastructure.
endef

define Build/Prepare
	$(Build/Prepare/Default)

	# Verify dependencies are the vendored version
	$(call EnsureVendoredVersion,../containerd/Makefile,containerd.installer)
	$(call EnsureVendoredVersion,../libnetwork/Makefile,proxy.installer)
	$(call EnsureVendoredVersion,../runc/Makefile,runc.installer)
	$(call EnsureVendoredVersion,../tini/Makefile,tini.installer)
endef

define Build/Configure
	# move so GoPackage/Build/Configure will get the correct path
	mv $(PKG_BUILD_DIR)/components/engine $(PKG_BUILD_DIR)/
	mv $(PKG_BUILD_DIR)/components/cli $(PKG_BUILD_DIR)/

	# docker generates files at build time so we'll just symlink for now and call GoPackage/Build/Configure later
	mkdir -p $(GO_PKG_BUILD_DIR)/bin \
			 $(GO_PKG_BUILD_DIR)/src \
			 $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/ \
			 $(GO_PKG_CACHE_DIR) \
			 $(GO_PKG_TMP_DIR)
	$(LN) $(PKG_BUILD_DIR)/cli $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/cli
	$(LN) $(PKG_BUILD_DIR)/engine $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/docker
endef

ifeq ($(CONFIG_DOCKER_SECCOMP),y)
BUILDTAGS:=seccomp
else
BUILDTAGS:=
endif

define Build/Compile
	( \
		export $(GO_PKG_VARS) \
			GITCOMMIT=$(PKG_SOURCE_VERSION) \
			DOCKER_GITCOMMIT=$(PKG_SOURCE_VERSION) \
			DOCKER_BUILDTAGS='$(BUILDTAGS)' \
			VERSION=$(PKG_VERSION) \
		\
		&& echo "Compiling CLI..." \
		&& cd $(PKG_BUILD_DIR)/cli \
		&& ./scripts/build/binary \
		\
		&& echo "Compiling Engine..." \
		&& cd $(PKG_BUILD_DIR)/engine \
		&& ./hack/make.sh binary \
	)

	# done here to include autogenerated files also
	rm $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/cli
	rm $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/docker
	$(call GoPackage/Build/Configure)
endef

define Package/docker-ce/install
	$(CP) ./files/* $(1)/
	$(INSTALL_DIR) $(1)/usr/bin/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/cli/build/docker $(1)/usr/bin/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/engine/bundles/binary-daemon/dockerd $(1)/usr/bin/

	$(INSTALL_DIR) $(1)/opt/docker/
	$(INSTALL_DIR) $(1)/usr/share/docker/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/engine/contrib/check-config.sh $(1)/usr/share/docker/

	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/dockerd.init $(1)/etc/init.d/dockerd

	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/etc/config/dockerd $(1)/etc/config/dockerd

	# Must be after systcl 11-br-netfilter.conf from kmod-br-netfilter
	$(INSTALL_DIR) $(1)/etc/sysctl.d
	$(INSTALL_DATA) ./files/etc/sysctl.d/sysctl-br-netfilter-ip.conf \
		$(1)/etc/sysctl.d/12-br-netfilter-ip.conf
endef

$(eval $(call BuildPackage,docker-ce))
